// 获取页面尺寸，用于判断是移动端还是PC端
let pageWidth = document.body.clientWidth;
let loginBtn = "";
let emailLogin = "";
let registerBtn = "";
let emails = "";
let password = "";
// 根据响应式布局分别获取数据
if (pageWidth >= 768) {
    loginBtn = document.getElementsByClassName("loginBtn")[0];
    emailLogin = document.getElementsByClassName("emailLogin")[0];
    registerBtn = document.getElementsByClassName("registerBtn")[0];
} else {
    loginBtn = document.getElementsByClassName("loginBtn")[1];
    emailLogin = document.getElementsByClassName("emailLogin")[1];
    registerBtn = document.getElementsByClassName("registerBtn")[1];
}

function pcLoginBtn() {
    emails = document.getElementsByClassName("email")[0].value;
    password = document.getElementsByClassName("psw")[0].value;
    reqDeal(emails, password);
}

function mbLoginBtn() {
    emails = document.getElementsByClassName("email")[1].value;
    password = document.getElementsByClassName("psw")[1].value;
    reqDeal(emails, password);
}

function reqDeal(emails, password) {
    ajax({
        type: 'POST',
        url: api.pswLogin,
        async: true,
        contentType: "application/json;charset=utf-8",
        data: {
            email: emails,
            password: password
        },
        success: function (data) {
            let res = JSON.parse(data);
            if (res.statusCode === 200) {
                alertBySweet("登录成功！");
                sessionStorage.setItem("userEmail",emails);
                location.href = route.noviceGuidance;
            } else {
                alertBySweet("登录失败，请检查邮箱和密码是否正确！");
            }
        },
        error: function () {
            alertBySweet("发送失败");
        }
    });
}

/*跳转至邮箱登录页面*/
emailLogin.addEventListener("click", function () {
    location.href = route.emailLogin;
});

/*跳转至注册页面*/
registerBtn.addEventListener('click', () => {
    location.href = route.register;
})